home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / MATH / MATH_LIB / PCOMPLEX.PAS < prev    next >
Pascal/Delphi Source File  |  1995-05-29  |  12KB  |  428 lines

  1. Unit PCOMPLEX;
  2.  
  3. (* Biblioptheque mathematique pour type complexe *)
  4. (* Version a fonction *)
  5. (* JD GAYRARD mai 95 *)
  6.  
  7. (* This library is based on functions instead of procedures.
  8.    To allow a function to return complex type, the trick is
  9.    is to use a pointer on the result of the function. All
  10.    function are of Pcomplex type (^complexe).
  11.    In the main program the function computation is accessed
  12.    by      z := function_name(param1, param2)^ *)
  13.  
  14. interface
  15.  
  16. uses MATHLIB, HYPERBOL;
  17.  
  18. type complexe = record
  19.                 reel : real;
  20.                 imag : real
  21.                 end;
  22.  
  23. pcomplexe = ^complexe;
  24.  
  25. const _i : complexe = (reel : 0.0; imag : 1.0);
  26.       _0 : complexe = (reel : 0.0; imag : 0.0);
  27.  
  28. var result : complexe;
  29.  
  30. (* quatre operations : +, -, * , / *)
  31. function cadd (z1, z2 : complexe) : pcomplexe;      (* addition *)
  32. function csub (z1, z2 : complexe) : pcomplexe;      (* soustraction *)
  33. function cmul (z1, z2 : complexe) : pcomplexe;      (* multiplication *)
  34. function cdiv (znum, zden : complexe) : pcomplexe;  (* division *)
  35.  
  36. (* fonctions complexes particulieres *)
  37. function cneg (z : complexe) : pcomplexe;  (* negatif *)
  38. function ccong (z : complexe) : pcomplexe; (* conjuge *)
  39. function crcp (z : complexe) : pcomplexe;  (* inverse *)
  40. function ciz (z : complexe) : pcomplexe;   (* multiplication par i *)
  41. function c_iz (z : complexe) : pcomplexe;  (* multiplication par -i *)
  42. function czero : pcomplexe;                (* return zero *)
  43.  
  44. (* fonctions complexes a retour non complexe *)
  45. function cmod (z : complexe) : real;           (* module *)
  46. function cequal (z1, z2 : complexe) : boolean; (* compare deux complexes *)
  47. function carg (z : complexe) : real;           (* argument : 0 / z = p ei0 *)
  48.  
  49. (* fonctions elementaires *)
  50. function cexp (z : complexe) : pcomplexe;  (* exponantielle *)
  51. function cln (z : complexe) : pcomplexe;  (* logarithme naturel *)
  52. function csqrt (z : complexe) : pcomplexe; (* racine carre *)
  53.  
  54. (* fonctions trigonometrique directe *)
  55. function ccos (z : complexe) : pcomplexe;  (* cosinus *)
  56. function csin (z : complexe) : pcomplexe;  (* sinus *)
  57. function ctg  (z : complexe) : pcomplexe;  (* tangente *)
  58.  
  59. (* fonctions trigonometriques inverses *)
  60. function carc_cos (z : complexe) : pcomplexe; (* arc cosinus *)
  61. function carc_sin (z : complexe) : pcomplexe; (* arc sinus *)
  62. function carc_tg  (z : complexe) : pcomplexe; (* arc tangente *)
  63.  
  64. (* fonctions trigonometrique hyperbolique *)
  65. function cch (z : complexe) : pcomplexe; (* cosinus hyperbolique *)
  66. function csh (z : complexe) : pcomplexe; (* sinus hyperbolique *)
  67. function cth (z : complexe) : pcomplexe; (* tangente hyperbolique *)
  68.  
  69. (* fonctions trigonometrique hyperbolique inverse *)
  70. function carg_ch (z : complexe) : pcomplexe; (* arc cosinus hyperbolique *)
  71. function carg_sh (z : complexe) : pcomplexe; (* arc sinus hyperbolique *)
  72. function carg_th (z : complexe) : pcomplexe; (* arc tangente hyperbolique *)
  73.  
  74.  
  75.  
  76. implementation
  77.  
  78. (* quatre operations de base +, -, * , / *)
  79.  
  80. function cadd (z1, z2 : complexe) : pcomplexe;
  81. (* addition : r := z1 + z2 *)
  82. begin
  83. result.reel := z1.reel + z2.reel;
  84. result.imag := z1.imag + z2.imag;
  85. cadd := @result
  86. end;
  87.  
  88. function csub (z1, z2 : complexe) : pcomplexe;
  89. (* soustraction : r :=  z1 - z2 *)
  90. begin
  91. result.reel := z1.reel - z2.reel;
  92. result.imag := z1.imag - z2.imag;
  93. csub := @result
  94. end;
  95.  
  96. function cmul (z1, z2 : complexe) : pcomplexe;
  97. (* multiplication : r := z1 * z2 *)
  98. begin
  99. result.reel := (z1.reel * z2.reel) - (z1.imag * z2.imag);
  100. result.imag := (z1.reel * z2.imag) + (z1.imag * z2.reel);
  101. cmul := @result
  102. end;
  103.  
  104. function cdiv (znum, zden : complexe) : pcomplexe;
  105. (* division : r := znum / zden *)
  106. var denom : real;
  107. begin
  108. with zden do denom := (reel * reel) + (imag * imag);
  109. if denom = 0.0
  110.    then begin
  111.         writeln('******** function Cdiv ********');
  112.         writeln('******* DIVISION PAR ZERO ******');
  113.         halt
  114.         end
  115.    else begin
  116.         result.reel := ((znum.reel * zden.reel) + (znum.imag * zden.imag)) / denom;
  117.         result.imag := ((znum.imag * zden.reel) - (znum.reel * zden.imag)) / denom
  118.         end;
  119. cdiv := @result
  120. end;
  121.  
  122. (* fonctions complexes particulieres *)
  123.  
  124. function cneg (z : complexe) : pcomplexe;
  125. (* negatif : r = - z *)
  126. begin
  127. result.reel := - z.reel;
  128. result.imag := - z.imag;
  129. cneg := @result
  130. end;
  131.  
  132. function cmod (z : complexe): real;
  133. (* module : r = |z| *)
  134. begin
  135. with z do cmod := sqrt((reel * reel) + (imag * imag))
  136. end;
  137.  
  138. function carg (z : complexe): real;
  139. (* argument : 0 / z = p ei0 *)
  140. begin
  141. carg := arctan2(z.reel, z.imag)
  142. end;
  143.  
  144. function ccong (z : complexe) : pcomplexe;
  145. (* conjuge : z := x + i.y alors r = x - i.y *)
  146. begin
  147. result.reel := z.reel;
  148. result.imag := - z.imag;
  149. ccong := @result
  150. end;
  151.  
  152. function crcp (z : complexe) : pcomplexe;
  153. (* inverse : r := 1 / z *)
  154. var denom : real;
  155. begin
  156. with z do denom := (reel * reel) + (imag * imag);
  157. if denom = 0.0
  158.    then begin
  159.         writeln('******** function Crcp ********');
  160.         writeln('******* DIVISION PAR ZERO ******');
  161.         halt
  162.         end
  163.    else begin
  164.         result.reel := z.reel / denom;
  165.         result.imag := - z.imag / denom
  166.         end;
  167. crcp := @result
  168. end;
  169.  
  170. function ciz (z : complexe) : pcomplexe;
  171. (* multiplication par i *)
  172. (* z = x + i.y , r = i.z = - y + i.x *)
  173. begin
  174. result.reel := - z.imag;
  175. result.imag := z.reel;
  176. ciz := @result
  177. end;
  178.  
  179. function c_iz (z : complexe) : pcomplexe;
  180. (* multiplication par -i *)
  181. (* z = x + i.y , r = i.z = y - i.x *)
  182. begin
  183. result.reel := z.imag;
  184. result.imag := - z.reel;
  185. c_iz := @result
  186. end;
  187.  
  188. function czero : pcomplexe;
  189. (* return a zero complexe *)
  190. begin
  191. result.reel := 0.0;
  192. result.imag := 0.0;
  193. czero := @result
  194. end;
  195.  
  196. function cequal (z1, z2 : complexe) : boolean;
  197. (* retourne TRUE si z1 = z2 *)
  198. begin
  199. cequal := (z1.reel = z2.reel) and (z1.imag = z2.imag)
  200. end;
  201.  
  202. (* fonctions elementaires *)
  203.  
  204. function cexp (z : complexe) : pcomplexe;
  205. (* exponantielle : r := exp(z) *)
  206. (* exp(x + iy) = exp(x).exp(iy) = exp(x).[cos(y) + i sin(y)] *)
  207. var expz : real;
  208. begin
  209. expz := exp(z.reel);
  210. result.reel := expz * cos(z.imag);
  211. result.imag := expz * sin(z.imag);
  212. cexp := @result
  213. end;
  214.  
  215. function cln (z : complexe) : pcomplexe;
  216. (* logarithme naturel : r := ln(z) *)
  217. (* ln( p exp(i0)) = ln(p) + i0 + 2kpi *)
  218. var modz : real;
  219. begin
  220. with z do modz := (reel * reel) + (imag * imag);
  221. if modz = 0.0
  222.    then begin
  223.         writeln('********* function Cln *********');
  224.         writeln('****** LOGARITHME DE ZERO ******');
  225.         halt
  226.         end
  227.    else begin
  228.    result.reel := ln(modz);
  229.    result.imag := arctan2(z.reel, z.imag);
  230.    cln := @result
  231.         end
  232. end;
  233.  
  234. function csqrt (z : complexe) : pcomplexe;
  235. (* racine carre : r := sqrt(z) *)
  236. var root, q : real;
  237. begin
  238. if (z.reel <> 0.0) or (z.imag <> 0.0)
  239.    then begin
  240.         root := sqrt(0.5 * (abs(z.reel) + cmod(z)));
  241.         q := z.imag / (2.0 * root);
  242.         if z.reel >= 0.0
  243.            then with result do
  244.                 begin
  245.                 reel := root;
  246.                 imag := q
  247.                 end
  248.            else if z.imag < 0.0
  249.                    then with result do
  250.                         begin
  251.                         reel := - q;
  252.                         imag := - root
  253.                         end
  254.                    else with result do
  255.                         begin
  256.                         reel :=  q;
  257.                         imag :=  root
  258.                         end
  259.         end
  260.    else result := z;
  261. csqrt := @result
  262. end;
  263.  
  264. (* fonctions trigonometriques directes *)
  265.  
  266. function ccos (z : complexe) : pcomplexe;
  267. (* cosinus complexe *)
  268. (* cos(x+iy) = cos(x).cos(iy) - sin(x).sin(iy) *)
  269. (* cos(ix) = ch(x) et sin(ix) = i.sh(x) *)
  270. begin
  271. result.reel := cos(z.reel) * ch(z.imag);
  272. result.imag := - sin(z.reel) * sh(z.imag);
  273. ccos := @result
  274. end;
  275.  
  276. function csin (z : complexe) : pcomplexe;
  277. (* sinus complexe *)
  278. (* sin(x+iy) = sin(x).cos(iy) + cos(x).sin(iy) *)
  279. (* cos(ix) = ch(x) et sin(ix) = i.sh(x) *)
  280. begin
  281. result.reel := sin(z.reel) * ch(z.imag);
  282. result.imag := cos(z.reel) * sh(z.imag);
  283. csin := @result
  284. end;
  285.  
  286. function ctg (z : complexe) : pcomplexe;
  287. (* tangente *)
  288. var ccosz, temp : complexe;
  289. begin
  290. ccosz := ccos(z)^;
  291. if (ccosz.reel = 0.0) and (ccosz.imag = 0.0)
  292.    then begin
  293.         writeln('********* function Ctg *********');
  294.         writeln('******* DIVISION PAR ZERO ******');
  295.         halt
  296.         end
  297.    else begin
  298.         temp := csin(z)^;
  299.         result := cdiv(temp, ccosz)^;
  300.         ctg := @result
  301.         end
  302. end;
  303.  
  304. (* fonctions trigonometriques inverses *)
  305.  
  306. function carc_cos (z : complexe) : pcomplexe;
  307. (* arc cosinus complexe *)
  308. (* arccos(z) = -i.argch(z) *)
  309. begin
  310. z := carg_ch(z)^;
  311. result := c_iz(z)^;
  312. carc_cos := @result
  313. end;
  314.  
  315. function carc_sin (z : complexe) : pcomplexe;
  316. (* arc sinus complexe *)
  317. (* arcsin(z) = -i.argsh(i.z) *)
  318. begin
  319. z := ciz(z)^;
  320. z := carg_sh(z)^;
  321. result := c_iz(z)^;
  322. carc_sin := @result
  323. end;
  324.  
  325. function carc_tg (z : complexe) : pcomplexe;
  326. (* arc tangente complexe *)
  327. (* arctg(z) = -i.argth(i.z) *)
  328. begin
  329. z := ciz(z)^;
  330. z := carg_th(z)^;
  331. result := c_iz(z)^;
  332. carc_tg := @result
  333. end;
  334.  
  335. (* fonctions trigonometriques hyperboliques *)
  336.  
  337. function cch (z : complexe) : pcomplexe;
  338. (* cosinus hyperbolique *)
  339. (* ch(x+iy) = ch(x).ch(iy) + sh(x).sh(iy) *)
  340. (* ch(iy) = cos(y) et sh(iy) = i.sin(y) *)
  341. begin
  342. result.reel := ch(z.reel) * cos(z.imag);
  343. result.imag := sh(z.reel) * sin(z.imag);
  344. cch := @result
  345. end;
  346.  
  347. function csh (z : complexe) : pcomplexe;
  348. (* sinus hyperbolique *)
  349. (* sh(x+iy) = sh(x).ch(iy) + ch(x).sh(iy) *)
  350. (* ch(iy) = cos(y) et sh(iy) = i.sin(y) *)
  351. begin
  352. result.reel := sh(z.reel) * cos(z.imag);
  353. result.imag := ch(z.reel) * sin(z.imag);
  354. csh := @result
  355. end;
  356.  
  357. function cth (z : complexe) : pcomplexe;
  358. (* tangente hyperbolique complexe *)
  359. (* th(x) = sh(x) / ch(x) *)
  360. (* ch(x) > 1 qq x *)
  361. var temp : complexe;
  362. begin
  363. temp := cch(z)^;
  364. z := csh(z)^;
  365. result := cdiv(z, temp)^;
  366. cth := @result
  367. end;
  368.  
  369. (* fonctions trigonometriques hyperboliques inverses *)
  370.  
  371. function carg_ch (z : complexe) : pcomplexe;
  372. (*   arg cosinus hyperbolique    *)
  373. (*                          _________  *)
  374. (* argch(z) = -/+ ln(z + i.V 1 - z.z)  *)
  375. var temp : complexe;
  376. begin
  377. with temp do begin
  378.              reel := 1 - z.reel * z.reel + z.imag * z.imag;
  379.              imag := - 2 * z.reel * z.imag
  380.              end;
  381. temp := csqrt(temp)^;
  382. temp := ciz(temp)^;
  383. temp := cadd(temp, z)^;
  384. temp := cln(temp)^;
  385. result := cneg(temp)^;
  386. carg_ch := @result
  387. end;
  388.  
  389. function carg_sh (z : complexe) : pcomplexe;
  390. (*   arc sinus hyperbolique    *)
  391. (*                    ________  *)
  392. (* argsh(z) = ln(z + V 1 + z.z) *)
  393. var temp : complexe;
  394. begin
  395. with temp do begin
  396.              reel := 1 + z.reel * z.reel - z.imag * z.imag;
  397.              imag := 2 * z.reel * z.imag
  398.              end;
  399. temp := csqrt(temp)^;
  400. temp := cadd(temp, z)^;
  401. result := cln(temp)^;
  402. carg_sh := @result
  403. end;
  404.  
  405. function carg_th (z : complexe) : pcomplexe;
  406. (* arc tangente hyperbolique *)
  407. (* argth(z) = 1/2 ln((z + 1) / (1 - z)) *)
  408. var temp : complexe;
  409. begin
  410. with temp do begin
  411.              reel := 1 + z.reel;
  412.              imag := z.imag
  413.              end;
  414. with result do begin
  415.           reel := 1 - reel;
  416.           imag := - imag
  417.           end;
  418. result := cdiv(temp, result)^;
  419. with result do begin
  420.           reel := 0.5 * reel;
  421.           imag := 0.5 * imag
  422.           end;
  423. carg_th := @result
  424. end;
  425.  
  426.  
  427.  
  428. end.